<?php

namespace App\Http\Controllers\CRM\MiniAPP;

use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\MiniAPP\AssistantTypeRequest;
use App\Http\Resources\CRM\MiniAPP\AssistantTypeResource;
use App\Models\FakeAssistantType;
use Illuminate\Http\Request;

class AssistantTypeController extends Controller
{
    public function getList()
    {
        $types = FakeAssistantType::latest()->get();

        return AssistantTypeResource::collection($types);
    }

    public function getInfo(FakeAssistantType $type)
    {
        return new AssistantTypeResource($type);
    }

    public function create(AssistantTypeRequest $request)
    {
        $type = FakeAssistantType::create($request->only(['name']));

        return new AssistantTypeResource($type);
    }

    public function edit(FakeAssistantType $type, AssistantTypeRequest $request)
    {
        $type->update($request->only(['name']));

        return new AssistantTypeResource($type);
    }

    public function remove(FakeAssistantType $type)
    {
        if ($type->assistants()->exists())
            return ApiResponseHelper::json([], 400, '该类型下存在助理，无法删除');

        $type->delete();
        return ApiResponseHelper::json([]);
    }
}
